package com.healer.hungery.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.JobStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionManager;

@Configuration
@EnableBatchProcessing
public class FatherJob {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private Job childJob1Job;

    @Autowired
    private Job childJob2Job;

    @Autowired
    private JobLauncher launcher;


    /**
     * 返回的是Job类型的Step
     *
     * @return
     */
    @Bean
    public Job fatherJobJob(JobRepository repository, PlatformTransactionManager manager) {
        return jobBuilderFactory.get("fatherJobJob")
                .start(childJobOne(repository, manager))
                .next(childJobTwo(repository, manager))
                .build();
    }

    @Bean
    public Step childJobOne(JobRepository repository, PlatformTransactionManager manager) {
        return new JobStepBuilder(new StepBuilder("childJobOne"))
                .job(childJob1Job)
                .launcher(launcher)
                .repository(repository)
                .transactionManager(manager)
                .build();
    }

    @Bean
    public Step childJobTwo(JobRepository repository, PlatformTransactionManager manager) {
        return new JobStepBuilder(new StepBuilder("childJobTwo"))
                .job(childJob2Job)
                .launcher(launcher)
                .repository(repository)
                .transactionManager(manager)
                .build();
    }

}
